<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="addQ('0')">添加权限</el-button>
      <el-table row-key="id" default-expand-all :data="list">
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              v-if="row.type === 1"
              size="mini"
              type="text"
              @click="addQ(row.type)"
            >添加</el-button>
            <el-button size="mini" type="text" @click="editQ(row.id)">编辑</el-button>
            &nbsp;
            <el-popconfirm title="确认删除该行数据吗？" @onConfirm="confirmDel(row.id)">
              <el-button slot="reference" size="mini" type="text">删除</el-button>
            </el-popconfirm>

          </template>
        </el-table-column>
      </el-table>
      <!-- 放置一个弹层 用来编辑新增节点 -->
      <el-dialog :title="title" :visible.sync="showDialog">
        <!-- 表单 -->
        <el-form ref="formData" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" style="width:90%" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="upData">确定</el-button>
            <el-button size="small" @click="showDialog=false">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getPermission, delPermission, addPermission, onePermission, editaddPermission } from '@/api/permission'
import { ProcessingData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        type: '',
        pid: ''
      },
      title: '',
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }

    }
  },
  watch: {
    // 监听控制弹出框的数据
    showDialog(newval) {
      // console.log(newval)
      if (newval === false) {
        // this.$refs.formData.resetFields()
        console.log(this.formData)
        this.formData.name = ''
        this.formData.code = ''
        this.formData.description = ''
        this.formData.enVisible = '0'
      }
    }
  },
  created() {
    this.getPermission()
  },
  methods: {
    async getPermission() {
      const { data } = await getPermission()
      this.list = ProcessingData(data, 0)
      // console.log(this.list)
    },
    async  confirmDel(id) {
      await delPermission(id)
      this.$message.success('删除成功')
      this.getPermission()
    },
    addQ(type) {
      this.title = '添加权限'
      this.showDialog = true
      console.log(type)
      this.formData.pid = type
      this.formData.type = type
    },
    async  upData() {
      await this.$refs.formData.validate()
      if (this.title === '添加权限') {
        console.log(1)
        const res = await addPermission(this.formData)
        console.log(res)
        this.$message.success('新增成功')
        this.showDialog = false
        this.$refs.formData.resetFields()
        this.getPermission()
      } else if (this.title === '编辑权限') {
        console.log(2)
        await editaddPermission(this.formData)
        this.$message.success('修改成功')
        this.showDialog = false
        this.getPermission()
      }
    },
    async editQ(id) {
      const res = await onePermission(id)
      console.log(res)
      this.formData = res.data
      this.title = '编辑权限'
      this.showDialog = true
    }

  }
}

</script>
<style>
.btn-add {
  margin: 10px;
}
</style>
